<?php
namespace Zxf5115\Laravel\Label\Controllers\Platform;

use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Zxf5115\Laravel\Label\Logics\LabelLogic;
use Zxf5115\Laravel\Kernel\Traits\ValidatorTrait;
use Zxf5115\Laravel\Kernel\Controllers\Platform\PlatformController;

/**
 * @author zhangxiaofei [<1326336909@qq.com>]
 * @dateTime 2025-10-01
 *
 * 标签控制器类
 */
class LabelController extends PlatformController
{
  use ValidatorTrait;

  /**
   * 允许客户端查询的字段
   */
  protected array $allowParams = [
    'type',
    'create_time',
  ];

  /**
   * 关键字查询配置
   */
  protected array $allowKeywordParams = [
    'receive' => 'keyword',
    'match' => [
      'title',
    ]
  ];

  /**
   * 排序规则
   */
  protected array $sortRules = [
    ['key' => 'sort', 'value' => 'desc'],
    ['key' => 'create_time', 'value' => 'desc'],
    ['key' => 'id', 'value' => 'desc'],
  ];

  /**
   * 操作标签信息
   *
   * @param Request $request 用户请求参数
   * @return JsonResponse 响应数据
   */
  public function handle(Request $request, LabelLogic $logic) : JsonResponse
  {
    try
    {
      $messages = [
        'title.required' => '请您输入标签标题',
      ];

      $rules = [
        'title' => 'required',
      ];

      // 验证用户数据内容是否正确
      $validation = $this->validation($request, $messages, $rules);

      if(!$validation['status'])
      {
        return error($validation['message']);
      }

      // 标签数据处理
      $logic->handle($request, $this->modelClass);

      return success(__('kernel.handle_success'));
    }
    catch(\Exception $e)
    {
      // 记录异常信息
      record($e);

      return error(__('kernel.handle_failure'));
    }
  }
}
